package reactor;

import org.junit.Test;
import reactor.core.publisher.Flux;
import util.Util;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author yding5
 */
public class ImperativeVSDeclarative {

    @Test
    public void testImperative() {
        List<Integer> source = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        Iterator<Integer> iterator = source.iterator();
        while (iterator.hasNext()) {
            Integer number = iterator.next();
            try {
                Util.print(-number);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Util.print("处理完成");
    }

    @Test
    public void testDeclarative() {
        Flux<Integer> source = Flux.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        source.map(number -> -number).subscribe(
            // onNext
            number -> Util.print(number),
            // onError
            e -> e.printStackTrace(),
            // onComplete
            () -> Util.print("处理完成"));
    }
}
